package servlet.admin;

import com.alibaba.fastjson.JSONObject;
import service.admin.CardDelService;
import service.admin.impl.CardDelServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * CardDelServlet
 *
 * @author lenovo
 * @since 2023/9/5
 */
@WebServlet("/admin/cardDel")
public class CardDelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    //引入服务层
    CardDelService service = new CardDelServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=utf8");
        // 接收数据
        String id = req.getParameter("id");
        PrintWriter out = resp.getWriter();
        //数据初始化
        int delCardResult = 0;
        int delHistoryResult = 0;
        String code = "1";
        String msg = "error";
        JSONObject json = new JSONObject();
        JSONObject jsonData = new JSONObject();

        delHistoryResult = service.delBook(Integer.valueOf(id));
        jsonData.put("num",delHistoryResult);
        delCardResult = service.delCard(Integer.valueOf(id));
        if (delCardResult == 1){
            code = "0";
            msg = "删除借阅证成功";
        }
        json.put("code", code);
        json.put("msg", msg);
        json.put("data",jsonData.toJSONString());
        out.print(json.toJSONString());
        out.flush();
        out.close();
    }
}
